import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class NoticeService extends BaseService<any> {
  private readonly logger = new Logger(NoticeService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getList
   * 对应 PHP: NoticeService_admin::getList()
   * 逻辑类型: undefined - undefined
   */
  async getList() {
    // 基于PHP真实逻辑: getList
    // PHP原文: return (new CoreNoticeService())->getAddonList($this->site_id);     }      /**      * 获取消息内容      * @param string $key      * @return array      */   ...
return this.coreNoticeService.getAddonList(this.site_id);
    }

    /**
     * 获取消息内容
     * @param string key
     * @return */
    async getInfo(string key)
    {
        return this.coreNoticeService.getInfo(this.site_id, key);
    }

    /**
     * 修改消息模板字段(todo  注意  仅限程序内部调用,故不做验证)
     * @param string key
     * @param string field_type
     * @param value
     * @return bool
     */
    async modify(string key, string field_type, value)
    {
        const data = [
            const field_type => value
        ];
        return this.coreNoticeService.edit(this.site_id, key, data);
    }

    /**
     * 修改消息状态
     * @param string key
     * @param string type
     * @param int status
     */
    async editMessageStatus(string key, string type, int status)
    {
        if (!array_key_exists(type, NoticeTypeDict.getType())) throw new AdminBusinessException('NOTICE_TYPE_NOT_EXIST');
        if (!array_key_exists(key, NoticeDict.getNotice())) return fail('NOTICE_TYPE_NOT_EXIST');
        return this.coreNoticeService.edit(this.site_id, key, ['is_' . type => status)];
    }

    /**
     * 消息编辑
     * @param string key
     * @param string type
     * @param data
     */
    async edit(string key, string type, data)
    {
        if (!array_key_exists(type, NoticeTypeDict.getType())) throw new AdminBusinessException('NOTICE_TYPE_NOT_EXIST');
        if (!array_key_exists(key, NoticeDict.getNotice())) return fail('NOTICE_TYPE_NOT_EXIST'];
        const save_data = ['is_' . type => data.status);
        switch (type) {
            case NoticeTypeDict.SMS:
                save_data.sms_id = data.sms_id || '';
                break;
            case NoticeTypeDict.WECHAT:
                save_data.wechat_first = data.wechat_first || '';
                save_data.wechat_remark = data.wechat_remark || '';
                break;
            case NoticeTypeDict.WEAPP:
                break;
  }

  /**
   * getInfo
   * 对应 PHP: NoticeService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(key: string) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: return (new CoreNoticeService())->getInfo($this->site_id, $key);     }      /**      * 修改消息模板字段(todo  注意  仅限程序内部调用,故不做验证)      * @param string $key   ...
return this.coreNoticeService.getInfo(this.site_id, key);
    }

    /**
     * 修改消息模板字段(todo  注意  仅限程序内部调用,故不做验证)
     * @param string key
     * @param string field_type
     * @param value
     * @return bool
     */
    async modify(string key, string field_type, value)
    {
        const data = [
            const field_type => value
        ];
        return this.coreNoticeService.edit(this.site_id, key, data);
    }

    /**
     * 修改消息状态
     * @param string key
     * @param string type
     * @param int status
     */
    async editMessageStatus(string key, string type, int status)
    {
        if (!array_key_exists(type, NoticeTypeDict.getType())) throw new AdminBusinessException('NOTICE_TYPE_NOT_EXIST');
        if (!array_key_exists(key, NoticeDict.getNotice())) return fail('NOTICE_TYPE_NOT_EXIST');
        return this.coreNoticeService.edit(this.site_id, key, ['is_' . type => status)];
    }

    /**
     * 消息编辑
     * @param string key
     * @param string type
     * @param data
     */
    async edit(string key, string type, data)
    {
        if (!array_key_exists(type, NoticeTypeDict.getType())) throw new AdminBusinessException('NOTICE_TYPE_NOT_EXIST');
        if (!array_key_exists(key, NoticeDict.getNotice())) return fail('NOTICE_TYPE_NOT_EXIST'];
        const save_data = ['is_' . type => data.status);
        switch (type) {
            case NoticeTypeDict.SMS:
                save_data.sms_id = data.sms_id || '';
                break;
            case NoticeTypeDict.WECHAT:
                save_data.wechat_first = data.wechat_first || '';
                save_data.wechat_remark = data.wechat_remark || '';
                break;
            case NoticeTypeDict.WEAPP:
                break;
  }

  /**
   * modify
   * 对应 PHP: NoticeService_admin::modify()
   * 逻辑类型: undefined - undefined
   */
  async modify(key: string, field_type: string, value: string) {
    // 基于PHP真实逻辑: modify
    // PHP原文: $data = array(             $field_type => $value         );         return (new CoreNoticeService())->edit($this->site_id, $key, $data);     }      /*...
const data = [
            const field_type => value
        ];
        return this.coreNoticeService.edit(this.site_id, key, data);
    }

    /**
     * 修改消息状态
     * @param string key
     * @param string type
     * @param int status
     */
    async editMessageStatus(string key, string type, int status)
    {
        if (!array_key_exists(type, NoticeTypeDict.getType())) throw new AdminBusinessException('NOTICE_TYPE_NOT_EXIST');
        if (!array_key_exists(key, NoticeDict.getNotice())) return fail('NOTICE_TYPE_NOT_EXIST');
        return this.coreNoticeService.edit(this.site_id, key, ['is_' . type => status)];
    }

    /**
     * 消息编辑
     * @param string key
     * @param string type
     * @param data
     */
    async edit(string key, string type, data)
    {
        if (!array_key_exists(type, NoticeTypeDict.getType())) throw new AdminBusinessException('NOTICE_TYPE_NOT_EXIST');
        if (!array_key_exists(key, NoticeDict.getNotice())) return fail('NOTICE_TYPE_NOT_EXIST'];
        const save_data = ['is_' . type => data.status);
        switch (type) {
            case NoticeTypeDict.SMS:
                save_data.sms_id = data.sms_id || '';
                break;
            case NoticeTypeDict.WECHAT:
                save_data.wechat_first = data.wechat_first || '';
                save_data.wechat_remark = data.wechat_remark || '';
                break;
            case NoticeTypeDict.WEAPP:
                break;
  }

  /**
   * editMessageStatus
   * 对应 PHP: NoticeService_admin::editMessageStatus()
   * 逻辑类型: undefined - undefined
   */
  async editMessageStatus(key: number, type: number, status: number) {
    // 基于PHP真实逻辑: editMessageStatus
    // PHP原文: if (!array_key_exists($type, NoticeTypeDict::getType())) throw new AdminException('NOTICE_TYPE_NOT_EXIST');         if (!array_key_exists($key, Notice...
if (!array_key_exists(type, NoticeTypeDict.getType())) throw new AdminBusinessException('NOTICE_TYPE_NOT_EXIST');
        if (!array_key_exists(key, NoticeDict.getNotice())) return fail('NOTICE_TYPE_NOT_EXIST');
        return this.coreNoticeService.edit(this.site_id, key, ['is_' . type => status)];
    }

    /**
     * 消息编辑
     * @param string key
     * @param string type
     * @param data
     */
    async edit(string key, string type, data)
    {
        if (!array_key_exists(type, NoticeTypeDict.getType())) throw new AdminBusinessException('NOTICE_TYPE_NOT_EXIST');
        if (!array_key_exists(key, NoticeDict.getNotice())) return fail('NOTICE_TYPE_NOT_EXIST'];
        const save_data = ['is_' . type => data.status);
        switch (type) {
            case NoticeTypeDict.SMS:
                save_data.sms_id = data.sms_id || '';
                break;
            case NoticeTypeDict.WECHAT:
                save_data.wechat_first = data.wechat_first || '';
                save_data.wechat_remark = data.wechat_remark || '';
                break;
            case NoticeTypeDict.WEAPP:
                break;
  }

  /**
   * edit
   * 对应 PHP: NoticeService_admin::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(key: any[], type: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: if (!array_key_exists($type, NoticeTypeDict::getType())) throw new AdminException('NOTICE_TYPE_NOT_EXIST');         if (!array_key_exists($key, Notice...
if (!array_key_exists(type, NoticeTypeDict.getType())) throw new AdminBusinessException('NOTICE_TYPE_NOT_EXIST');
        if (!array_key_exists(key, NoticeDict.getNotice())) return fail('NOTICE_TYPE_NOT_EXIST'];
        const save_data = ['is_' . type => data.status);
        switch (type) {
            case NoticeTypeDict.SMS:
                save_data.sms_id = data.sms_id || '';
                break;
            case NoticeTypeDict.WECHAT:
                save_data.wechat_first = data.wechat_first || '';
                save_data.wechat_remark = data.wechat_remark || '';
                break;
            case NoticeTypeDict.WEAPP:
                break;
  }
}
